package com.sanshi.project.assets.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.log.annotation.SysLog;
import com.sanshi.project.admin.api.dto.InspectPointDTO;
import com.sanshi.project.admin.api.entity.InspectPointEntity;
import com.sanshi.project.admin.api.entity.InspectItemEntity;
import com.sanshi.project.assets.service.InspectPointService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 巡检点
 *
 * @author nikon
 * @date 2024-01-01 16:29:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/inspectPoint" )
@Tag(description = "inspectPoint" , name = "巡检点管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class InspectPointController {

    private final  InspectPointService inspectPointService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param inspectPoint 巡检点
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('assets_inspectPoint_view')" )
    public R getInspectPointPage(@ParameterObject Page page, @ParameterObject InspectPointDTO inspectPoint) {
        LambdaQueryWrapper<InspectPointEntity> wrapper = Wrappers.lambdaQuery();
        // 巡检点名称
		wrapper.like(StrUtil.isNotBlank(inspectPoint.getName()),InspectPointEntity::getName,inspectPoint.getName());
        // 地址
		wrapper.like(StrUtil.isNotBlank(inspectPoint.getAddress()),InspectPointEntity::getAddress,inspectPoint.getAddress());
        // 备注
		wrapper.like(StrUtil.isNotBlank(inspectPoint.getRemark()),InspectPointEntity::getRemark,inspectPoint.getRemark());
        return R.ok(inspectPointService.page(page, wrapper));
    }

    /**
     * 通过id查询巡检点
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('assets_inspectPoint_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(inspectPointService.getByIdDeep(id));
    }

    /**
     * 新增巡检点
     * @param inspectPoint 巡检点
     * @return R
     */
    @Operation(summary = "新增巡检点" , description = "新增巡检点" )
    @SysLog("新增巡检点" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_inspectPoint_add')" )
    public R save(@RequestBody InspectPointEntity inspectPoint) {
        return R.ok(inspectPointService.saveDeep(inspectPoint));
    }

    /**
     * 修改巡检点
     * @param inspectPoint 巡检点
     * @return R
     */
    @Operation(summary = "修改巡检点" , description = "修改巡检点" )
    @SysLog("修改巡检点" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_inspectPoint_edit')" )
    public R updateById(@RequestBody InspectPointEntity inspectPoint) {
        return R.ok(inspectPointService.updateDeep(inspectPoint));
    }

    /**
     * 通过id删除巡检点
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除巡检点" , description = "通过id删除巡检点" )
    @SysLog("通过id删除巡检点" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_inspectPoint_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(inspectPointService.removeDeep(ids));
    }

    /**
     * 通过id删除巡检点子表数据
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除巡检点子表数据" , description = "通过id删除巡检点子表数据" )
    @SysLog("通过id删除巡检点子表数据" )
    @DeleteMapping("/child")
    @PreAuthorize("@pms.hasPermission('assets_inspectPoint_del')" )
    public R removeChild(@RequestBody Long[] ids) {
        return R.ok(inspectPointService.removeChild(ids));
    }

    /**
     * 导出excel 表格
     * @param inspectPoint 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_inspectPoint_export')" )
    public List<InspectPointEntity> export(InspectPointEntity inspectPoint,Long[] ids) {
        return inspectPointService.list(Wrappers.lambdaQuery(inspectPoint).in(ArrayUtil.isNotEmpty(ids), InspectPointEntity::getId, ids));
    }
}